package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhentao.dao.IndexBannerDao;
import com.zhentao.pojo.Banner;
import com.zhentao.service.IndexPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexPublicServiceImpl implements IndexPublicService {
    @Autowired
    private IndexBannerDao indexBannerDao;

    @Override
    public java.util.List<Banner> listIndexBanners(Integer position, Integer bannerType, Integer status) {
        java.time.LocalDateTime now = java.time.LocalDateTime.now();

        LambdaUpdateWrapper<Banner> uwOnline = new LambdaUpdateWrapper<>();
        uwOnline.set(Banner::getStatus, 1)
                .eq(Banner::getStatus, 0)
                .and(w -> w.isNull(Banner::getStartTime).or().le(Banner::getStartTime, now))
                .and(w -> w.isNull(Banner::getEndTime).or().ge(Banner::getEndTime, now));
        indexBannerDao.update(null, uwOnline);

        LambdaUpdateWrapper<Banner> uwOffline = new LambdaUpdateWrapper<>();
        uwOffline.set(Banner::getStatus, 0)
                .eq(Banner::getStatus, 1)
                .isNotNull(Banner::getEndTime)
                .lt(Banner::getEndTime, now);
        indexBannerDao.update(null, uwOffline);
        
        LambdaQueryWrapper<Banner> qw = new LambdaQueryWrapper<>();
        qw.eq(Banner::getStatus, status)
                .and(w -> w.isNull(Banner::getStartTime).or().le(Banner::getStartTime, now))
                .and(w -> w.isNull(Banner::getEndTime).or().ge(Banner::getEndTime, now));
        qw.orderByAsc(Banner::getSortNum);
        return indexBannerDao.selectList(qw);
    }
}